import axios from 'axios'
import { getToken } from '~/composables/auth'
import { success } from '~/composables/util'
import { useStore } from 'vuex'


const store = useStore()




const instance = axios.create({
    baseURL: '/api'
  });


  // 添加请求拦截器
  instance.interceptors.request.use(function (config) {


    const token= getToken()
    if(token){
        config.headers["token"]=token
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    return response.data.data;
  }, function (error) {
    const msg=error.response.data.msg||'请求失败'

    if(msg=="非法token,请重新登录！"){
      store.dispatch("logout").finally(()=>location.reload())
    }
    success(error.response.data.msg||'请求失败','error')
    return Promise.reject(error);
  });

  export default instance